// pages/subpackage/withdrawDeposit/index.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: '1.00',
    creditCardName: '中国工商银行',
    creditCardNum: '55041234561412',
    concealCreditCardNum: '',
    withdrawCash: {},
    phone: wx.getStorageSync('TwoPlusTravel').phone || '',
    BankCardShow: false,
    withdrawCashShow: false,
    addCard: {
      name: '',
      bank: '',
      bank_number: '',
      Branch_office: ''
    },
    columns: ['中国工商银行', '招商银行', '中国农业银行', '中国建设银行', '中国银行', '中国民生银行', '中国光大银行', '中信银行', '交通银行', '兴业银行', '上海浦东发展银行', '中国人民银行', '中国邮政储蓄银行'],
    changebankShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setcreditCardNum()
  },
  setcreditCardNum () {
    let a = '55041234561412';
    // 获取下标为4-9的部分
    let middlePart = a.substring(4, 10);
    // 生成星号
    let stars = '*'.repeat(middlePart.length);
    // 替换中间部分为星号
    let result = a.substring(0, 4) + stars + a.substring(10);
    console.log(result);
    this.setData({
      concealCreditCardNum: result
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getMyWallet()
    this.getBankInformation()
  },
  getMyWallet () {
    let that = this
    util.request('GET', api.getMyWallet, {phone: this.data.phone}).then(function (res) {
      if (Object.keys(res).length) {
        that.setData({
          withdrawCash: res
        })
      }
      wx.stopPullDownRefresh()
    })
  },
  getBankInformation () {
    let that = this
    util.request('GET', api.getBankInformation).then(function (res) {
      let list = []
      if (res.length) {
        res.forEach((item) => {
          list.push(item.Bank)
        })
        that.setData({
          columns: list
        })
      }
    })
  },
  AddBankCard () {
    let addCard = {
      name: '',
      bank: '',
      bank_number: '',
      Branch_office: ''
    }
    this.setData({
      BankCardShow: !this.data.BankCardShow,
      addCard: addCard
    })
  },
  onChangename (e) {
    let addCard = this.data.addCard
    let index = e.currentTarget.dataset.index
    switch (index) {
      case '0':
        addCard.name = e.detail
        break;
      case '1':
        addCard.bank = e.detail
        break;
      case '2':
        addCard.Branch_office = e.detail
        break;
      case '3':
        addCard.bank_number = e.detail
        break;
      default:
        break;
    }
    this.setData({
      addCard: addCard
    })
  },
  AddBankCardConf () {
    let that = this
    let data = this.data.addCard
    if (data.name == '' || data.bank == '' || data.bank_number == '' || data.Branch_office == '') {
      wx.showToast({
        title: '请完善内容',
        icon: 'error'
      })
      return
    }
    data.phone = this.data.phone
    util.request('POST', api.postbankCard, data).then(function (res) {
      if (res.code == 200) {
        wx.showToast({
          title: '添加成功'
        })
        that.AddBankCard()
        that.getMyWallet()
      }else {
        wx.showToast({
          title: '添加失败 请稍后再试',
          icon: 'none'
        })
      }
      wx.stopPullDownRefresh
    })
  },
  BalanceWithdrawal () {
    let that = this
    if (Number(that.data.withdrawCash.money) <= 0) {
      wx.showToast({
        title: '余额需大于0',
        icon: 'error'
      })
      return
    }
    if (!that.data.withdrawCash.bank) {
      wx.showToast({
        title: '请添加银行卡',
        icon: 'error'
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '是否确认提现',
      success (res) {
        if (res.confirm) {
          let data = {
            phone: that.data.phone,
            money: that.data.withdrawCash.money
          }
          util.request('POST', api.postWithdrawal, data).then(function (res) {
            if (res.code == 200) {
              that.setData({
                money: that.data.withdrawCash.money,
                withdrawCashShow: true
              })
              that.getMyWallet()
            }else if(res.code == 100) {
              wx.showToast({
                title: '每日只能提现一次',
                icon: 'error'
              })
            }else {
              wx.showToast({
                title: '提现失败 请联系管理员',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },
  WithdrawalSuccess () {
    this.setData({
      withdrawCashShow: !this.data.withdrawCashShow
    })
  },
  onDisplay () {
    this.setData({
      changebankShow: !this.data.changebankShow
    })
  },
  onChange(event) {
    const { picker, value, index } = event.detail;
    let addCard = this.data.addCard
    addCard.bank = value
    console.log(addCard);
    this.setData({
      addCard: addCard,
      changebankShow: false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    let time = setTimeout(() => {
      this.getMyWallet()
      this.getBankInformation()
      clearTimeout(time)
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})